using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Attack : MonoBehaviour
{
    public Collider2D attackCollider;
    public float damage = 3;
    Vector2 rightAttackOffset;
    private GameObject player;

    public AudioClip audioClip;
    private AudioSource audioSource;

    private void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        audioSource = player.GetComponent<AudioSource>();
        rightAttackOffset = transform.position;
        print(rightAttackOffset);
    }

    public void AttackRight()
    {
        print("r");
        audioSource.PlayOneShot(audioClip);
        attackCollider.enabled = true;
        transform.localPosition = rightAttackOffset;
        print(transform.localPosition);
    }

    public void AttackLeft()
    {
        print("l");
        audioSource.PlayOneShot(audioClip);
        attackCollider.enabled = true;
        transform.localPosition = new Vector3(rightAttackOffset.x * -1, rightAttackOffset.y);
        print(transform.localPosition);
    }

    public void StopAttack()
    {
        attackCollider.enabled = false;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Enemy")
        {
            Enemy enemy = other.GetComponent<Enemy>();

            audioSource.PlayOneShot(audioClip);

            if (enemy != null)
            {
                enemy.Health -= damage;
            }
        }
    }
}
